//
//  QYPhotoMaskChooseManage.m
//  QingYue
//
//  Created by Zheng on 2017/5/23.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYPhotoMaskChooseManager.h"

@interface QYPhotoMaskChooseManager ()

@property (nonatomic, strong) UIView *maskChooseView;
@property (nonatomic, strong) UIButton *takePhotoBtn;
@property (nonatomic, strong) UIButton *albumPhotoBtn;
@property (nonatomic, strong) UIButton *cancelBtn;

@end

@implementation QYPhotoMaskChooseManager

+ (void)initialize {
    [QYPhotoMaskChooseManager defaultManager];
}

+ (instancetype)defaultManager {
    static QYPhotoMaskChooseManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[QYPhotoMaskChooseManager alloc] init];
    });
    return instance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [[UIApplication sharedApplication].delegate.window addSubview:self.maskChooseView];
    }
    return self;
}

- (void)showPhotoMaskChooseView {
    if (_maskChooseView.hidden) {
        [UIView animateWithDuration:.35f animations:^{
            _maskChooseView.hidden = NO;
            _maskChooseView.alpha = 1;
        } completion:^(BOOL finished) {
            
        }];
    } else {
        [UIView animateWithDuration:.35f animations:^{
            _maskChooseView.alpha = 0;
        } completion:^(BOOL finished) {
            _maskChooseView.hidden = YES;
        }];
    }
}

#pragma mark - Button 点击事件
- (void)buttonActionInMaskView:(UIButton *)sender {
    [self showPhotoMaskChooseView];

    if (sender == _takePhotoBtn) {
        _gotoPhotoTakeBlock();
    } else if (sender == _albumPhotoBtn) {
        _gotoPhotoLibBlock();
    }
}


#pragma mark - lazy load
- (UIView *)maskChooseView {
    if (!_maskChooseView) {
        _maskChooseView = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].delegate.window.bounds];
        _maskChooseView.backgroundColor = [UIColor colorWithWhite:0.6 alpha:0.5];
        _maskChooseView.hidden = YES;
        _maskChooseView.alpha = 0.f;
        
        [_maskChooseView addSubview:self.cancelBtn];
        [_maskChooseView addSubview:self.albumPhotoBtn];
        [_maskChooseView addSubview:self.takePhotoBtn];
        
        [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(_maskChooseView);
            make.height.mas_equalTo(50);
        }];
        
        [_albumPhotoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(_maskChooseView);
            make.height.equalTo(_cancelBtn);
            make.bottom.equalTo(_cancelBtn.mas_top).offset(-8);
        }];
        
        [_takePhotoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(_maskChooseView);
            make.height.equalTo(_cancelBtn);
            make.bottom.equalTo(_albumPhotoBtn.mas_top).offset(-1);
        }];
        
    }
    return _maskChooseView;
}

- (UIButton *)takePhotoBtn {
    if (!_takePhotoBtn) {
        _takePhotoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _takePhotoBtn.titleLabel.font = Font(17);
        [_takePhotoBtn setTitle:@"拍照选择" forState:UIControlStateNormal];
        [_takePhotoBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _takePhotoBtn.backgroundColor = [UIColor whiteColor];
        [_takePhotoBtn addTarget:self action:@selector(buttonActionInMaskView:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _takePhotoBtn;
}

- (UIButton *)albumPhotoBtn {
    if (!_albumPhotoBtn) {
        _albumPhotoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _albumPhotoBtn.titleLabel.font = Font(17);
        [_albumPhotoBtn setTitle:@"相册选择" forState:UIControlStateNormal];
        [_albumPhotoBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _albumPhotoBtn.backgroundColor = [UIColor whiteColor];
        [_albumPhotoBtn addTarget:self action:@selector(buttonActionInMaskView:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _albumPhotoBtn;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelBtn.titleLabel.font = Font(17);
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _cancelBtn.backgroundColor = [UIColor whiteColor];
        [_cancelBtn addTarget:self action:@selector(buttonActionInMaskView:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}



@end
